//
//  HMPopView.swift
//  EmotionKeybaord
//
//  Created by ZhuDong on 16/6/1.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

let scale = UIScreen.mainScreen().scale
class HMPopView: UIView {
    var image = UIImage(named:"emoticon_keyboard_magnifier")!
    override init(frame:CGRect){
        let image = self.image
        let w = image.size.width
        let h = image.size.height
        let rect = CGRect(x: 0,y: 0,width: w,height: h)
        super.init(frame:rect)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func setupUI(){
        self.addSubview(imageView)
        imageView.snp_makeConstraints { (make) in
            make.edges.equalTo(self)
        }
        addSubview(btn)
        btn.snp_makeConstraints { (make) in
            make.top.left.right.equalTo(self)
            make.height.equalTo(image.size.height / 2)
        }
    }
    func show(btn:HMEmotionButton){
        if self.superview == nil {
            windowOne?.addSubview(self)
        }
        self.hidden = false
        let frame = btn.convertRect(btn.bounds, toView: window)
        let x = (frame.size.width - bounds.width) * 0.5 + frame.origin.x
        let y = frame.maxY - bounds.height
        self.frame = CGRect(x: x, y: y, width: bounds.width, height: bounds.height)
        self.emotion = btn.emotion
        
    }
    func dismissAfter(){
            let time_t = dispatch_time(DISPATCH_TIME_NOW, Int64(Double(NSEC_PER_SEC) * 1))
            dispatch_after(time_t, dispatch_get_main_queue()) {
                self.hidden = true
            }
    }
    var emotion:HMEmotion?{
        didSet{
            let image = UIImage(named: (emotion?.imagePath)!)
            btn.setImage(image, forState: .Normal)
        }
    }
    private lazy var btn: UIButton = {
        let btn = UIButton()
        return btn
    }()
    private lazy var imageView: UIImageView = {
        let i = UIImageView()
        i.image = self.image
//        i.backgroundColor = UIColor.redColor()
       return i
    }()
}
